package com.goodjia.phase1module3;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class StudentMapTest {

    public static void main(String[] args) {

        // 创建map添加三个键值对
        Map<Student, String> m1 = new HashMap<>();
        m1.put(new Student("小王",18),"深圳");
        m1.put(new Student("小李",19),"北京");
        m1.put(new Student("小刘",20),"上海");

        // 1.第一种方式entrySet遍历 在键和值都需要时
        for (Map.Entry<Student, String> entry : m1.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
        }

        System.out.println("-----------------------------------------------");
        // 2.第二种方式 遍历map中的键
        for (Student student : m1.keySet()) {
            System.out.println("key = " + student);
        }

        System.out.println("-----------------------------------------------");
        // 3.第三种方式 遍历map中的值
        for (String value : m1.values()) {
            System.out.println("value = " + value);
        }

        System.out.println("-----------------------------------------------");
        // 4.通过迭代器遍历
        Iterator<Map.Entry<Student, String>> it = m1.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Student, String> entry = it.next();
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
        }
    }
}
